package com.baijia.util;

import com.baijia.passport.core.RedisTemplate;
import com.baijia.passport.core.RedisTemplate.Callback;

import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

@Component
// @Data
public class RedisUtil {

    // private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);

    // 操作成功
    public static final int SUCCESS = 1;
    // 操作失败
    public static final int FAIL = 0;

    // private static JedisPool jedisPool;
    // @Value("${redis.host}")
    // private String host;
    // @Value("${redis.port:6379}")
    // private int port;
    // @Value("${redis.password:#{null}}")
    // private String password;
    // @Value("${redis.timeout:3000}")
    // private int timeout;
    // @Value("${redis.maxIdle:30000}")
    // private int maxIdle;
    // @Value("${redis.default.db:0}")
    // private int db;
    // @Value("${redis.max_retry:5}")
    // private int maxRetry;
    //
    // @PostConstruct
    // public void init() throws URISyntaxException {
    // logger.info("Try to init redis");
    // GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    // poolConfig.setMaxIdle(maxIdle);
    // poolConfig.setTestOnBorrow(true);
    // // JedisShardInfo shardInfo = null;
    // // redis://:password@host:port/db
    // StringBuilder uri = new StringBuilder("redis://");
    // if (StringUtils.isNotBlank(password)) {
    // uri.append(":").append(password).append("@");
    // }
    // uri.append(host).append(":");
    // uri.append(port).append("/");
    // uri.append(db);
    // jedisPool = new JedisPool(poolConfig, new URI(uri.toString()));
    // }
    //
    // private static Jedis getJedis() {
    // return jedisPool.getResource();
    // }
    //
    // private static void returnResource(Jedis jedis) {
    // jedis.close();
    // }

    public static void setex(final byte[] key, final int seconds, final byte[] value) {
        // Jedis jedis = getJedis();
        // jedis.setex(key, seconds, value);
        // returnResource(jedis);
        RedisTemplate.execute(new Callback<Void>() {
            @Override
            public Void doInInstance(Jedis jedis) {
                jedis.setex(key, seconds, value);
                return null;
            }
        });
    }

    public static void set(final byte[] key, final byte[] value) {
        // Jedis jedis = getJedis();
        // jedis.set(key, value);
        // returnResource(jedis);
        RedisTemplate.execute(new Callback<Void>() {
            @Override
            public Void doInInstance(Jedis jedis) {
                jedis.set(key, value);
                return null;
            }
        });
    }

    public static byte[] get(final byte[] key) {
        // Jedis jedis = RedisUtil.getJedis();
        // byte[] value = jedis.get(key);
        // RedisUtil.returnResource(jedis);
        // return value;
        return RedisTemplate.execute(new Callback<byte[]>() {
            @Override
            public byte[] doInInstance(Jedis jedis) {
                return jedis.get(key);
            }
        });
    }

    public static boolean del(final byte[] key) {
        // Jedis jedis = RedisUtil.getJedis();
        // boolean result = jedis.del(key) == SUCCESS;
        // RedisUtil.returnResource(jedis);
        // return result;
        return RedisTemplate.execute(new Callback<Boolean>() {
            @Override
            public Boolean doInInstance(Jedis jedis) {
                return jedis.del(key) == SUCCESS;
            }
        });
    }

    public static boolean hDel(final String key, final String field) {
        // Jedis jedis = RedisUtil.getJedis();
        // boolean result = jedis.hdel(key, field) == SUCCESS;
        // RedisUtil.returnResource(jedis);
        // return result;
        return RedisTemplate.execute(new Callback<Boolean>() {
            @Override
            public Boolean doInInstance(Jedis jedis) {
                return jedis.hdel(key, field) == SUCCESS;
            }
        });
    }

    public static void hSet(final String key, final String field, final String value, final int seconds) {
        // Jedis jedis = RedisUtil.getJedis();
        // jedis.hset(key, field, value);
        // jedis.expire(key, seconds);
        // RedisUtil.returnResource(jedis);
        RedisTemplate.execute(new Callback<Void>() {
            @Override
            public Void doInInstance(Jedis jedis) {
                jedis.hset(key, field, value);
                jedis.expire(key, seconds);
                return null;
            }
        });
    }

    public static String hGet(final String key, final String field) {
        // Jedis jedis = RedisUtil.getJedis();
        // String value = jedis.hget(key, field);
        // RedisUtil.returnResource(jedis);
        // if (value != null && value.equals("nil")) {
        // value = null;
        // }
        // return value;
        return RedisTemplate.execute(new Callback<String>() {
            @Override
            public String doInInstance(Jedis jedis) {
                String value = jedis.hget(key, field);
                if (value != null && value.equals("nil")) {
                    value = null;
                }
                return value;
            }
        });
    }

    public static Long incrBy(final String key, final String field, final long value) {
        // Jedis jedis = RedisUtil.getJedis();
        // Long newValue = jedis.hincrBy(key, field, value);
        // RedisUtil.returnResource(jedis);
        // return newValue;
        return RedisTemplate.execute(new Callback<Long>() {
            @Override
            public Long doInInstance(Jedis jedis) {
                return jedis.hincrBy(key, field, value);
            }
        });
    }
}
